package com.qqtech.qquan.mall.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.model.MallShopCommunity;
import com.qqtech.qquan.mall.model.MallShopCommunityVo;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopCommunityService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/mall/shop/community")
public class MallShopCommunityController extends BaseAgentController {
	@Resource
	private MallShopCommunityService mallShopCommunityService;
	@Resource
	private MallShopService mallShopService;

	@Override
	protected BaseService<MallShopCommunity> getBaseService() {
		return mallShopCommunityService;
	}

	@RequestMapping(value = "/edit/{shopId}", method = RequestMethod.GET)
	public String edit(@PathVariable Integer shopId, HttpServletRequest request,
			HttpServletResponse response) {
		if (shopId != null) {
			MallShopVo shop = mallShopService.getById(shopId);
			request.setAttribute("shop", shop);
			List<MallShopCommunityVo> mallShopCommunityVoList = mallShopCommunityService.queryListByShopId(shopId);
			request.setAttribute("mallShopCommunityVoList", mallShopCommunityVoList);
		}
		return "/mall/shop/community-edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(Integer shopId, String communityIds, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (shopId == null) {
			ret.addErr("shopId不能为空");
		}
		if (StringUtil.isBlank(communityIds)) {
			ret.addErr("至少选择一个社区");
		}
		String[] communityIdArray = communityIds.split(";");
		int length = communityIdArray.length;
		for (int i = 0; i < length - 1; i++) {
            String temp = communityIdArray[i];
            for (int j = i + 1; j < length; j++) {
                if (temp.equals(communityIdArray[j])) {
                	ret.addErr("第" + (i + 1) + "个跟第" + (j + 1) + "个重复 ");
                }
            }
        }
		if (ret.checkForm()) {
			List<Integer> communityIdList = new ArrayList<Integer>(length);
			for (int i = 0; i < length; i++) {
				communityIdList.add(Integer.valueOf(communityIdArray[i]));
			}
			int v = mallShopCommunityService.handleSave(shopId, communityIdList);
			if (v <= 0) {
				ret.addErr("更新失败，请联系管理员");
			}
		}
		return ret;
	}

}
